﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ManageSystem
{
    public partial class ManageBook : Form
    {
        public ManageBook()
        {
            InitializeComponent();
            initData();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (this.listView1.SelectedItems.Count > 0)
            {


                string readString = "select bookContent from bookInfo  where bookId=" + listView1.SelectedItems[0].SubItems[0].Text;
                SqlDataReader sqlDr = Utils.getDataReader(readString);
                //string test = listView1.SelectedItems[0].SubItems[0].Text;
                //test = listView1.SelectedItems[0].SubItems[1].Text;
                //test = listView1.SelectedItems[0].SubItems[2].Text;
                //test = listView1.SelectedItems[0].SubItems[3].Text;
                //test = listView1.SelectedItems[0].SubItems[4].Text;
                //test = listView1.SelectedItems[0].SubItems[5].Text;

                //try
                //{
                //    test = sqlDr.GetString(0);
                //}
                //catch
                //{
                //    test = "无";
                //}
                sqlDr.Read();



                AddBook addBook = new AddBook(1, listView1.SelectedItems[0].SubItems[0].Text, listView1.SelectedItems[0].SubItems[1].Text, listView1.SelectedItems[0].SubItems[2].Text, listView1.SelectedItems[0].SubItems[3].Text, sqlDr["bookContent"].ToString(), listView1.SelectedItems[0].SubItems[5].Text, listView1.SelectedItems[0].SubItems[4].Text);
                addBook.Owner = this;

                addBook.Show();
            }
            else
            {
                MessageBox.Show("请选择一本图书！");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            AddBook addBook = new AddBook(0);
            addBook.Owner = this;
            addBook.Show();
        }

        public void initData()
        {
            listView1.Items.Clear();
            SqlDataReader sdr = Utils.getDataReader("select * from bookInfo");
            while (sdr.Read())
            {

                ListViewItem lv = new ListViewItem(sdr[0].ToString());


                lv.SubItems.Add(sdr[1].ToString());
                lv.SubItems.Add(sdr[3].ToString());
                lv.SubItems.Add(sdr[4].ToString());
                lv.SubItems.Add(sdr[6].ToString());
                //if (((int)sdr[7]) == 1)
                //    lv.SubItems.Add("已借出");
                //else
                //    lv.SubItems.Add("未借出");
                lv.SubItems.Add(sdr[2].ToString());

                listView1.Items.Add(lv);
            }
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            initData();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 0)
            {
                MessageBox.Show("请选择要删除的图书！");
                return;
            }
            if (MessageBox.Show("是否要删除当前选中图书?", "删除询问", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                string delStr = "delete from bookInfo where bookId=" + listView1.SelectedItems[0].SubItems[0].Text;
                Utils.ExecuteNoQuery(delStr);
                initData();
            }
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}
